package p0;

import java.util.Arrays;
import java.util.Stack;

public class Leetcode_20_2 {

    /**
     * 老老实实用栈来做一遍
     *
     * @param s
     * @return
     */
    public static boolean isValid(String s) {
        Stack<String> stringStack = new Stack<>();
        for (String s1 : s.split("")) {
            if ("(".equals(s1) || "{".equals(s1) || "[".equals(s1)) {
                stringStack.push(s1);
            } else if (!stringStack.empty()) {
                if (")".equals(s1)) {
                    String pop = stringStack.pop();
                    if (!"(".equals(pop)) {
                        return false;
                    }
                } else if ("}".equals(s1)) {
                    String pop = stringStack.pop();
                    if (!"{".equals(pop)) {
                        return false;
                    }
                } else if ("]".equals(s1)) {
                    String pop = stringStack.pop();
                    if (!"[".equals(pop)) {
                        return false;
                    }
                }
            } else {
                return false;
            }
        }
        return stringStack.empty();
    }

    public static void main(String[] args) {
        System.out.println(isValid("}"));
    }
}
